﻿using System;
using System.Web.Mvc;
using HiLand.General.BLL;
using HiLand.General.Entity;
using HiLand.Utility.Entity;
using HiLand.Utility.Enums;
using HiLand.Utility.Paging;
using HiLand.Utility4.MVC.Data;
using Legou365.Web.Models;
using Webdiyer.WebControls.Mvc;

namespace Legou365.Web.Areas.Manage.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 产品类别列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ProductCategoryList()
        {
            return View();
        }

        /// <summary>
        /// 产品列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ProductList(int id = 1)
        {
            int pageIndex = id;
            int pageSize = Miscs.PageSizeManage;
            int startIndex = (pageIndex - 1) * pageSize + 1;
            string whereClause = " 1=1 "; //string.Format(" LoanType= {0} AND LoanStatus!={1}  ",                (int)loanType, (int)LoanStatuses.UserUnCompleted);
            string orderClause = "productID DESC";

            PagedEntityCollection<SimpleProductEntity> entityList = SimpleProductBLL.Instance.GetPagedCollection(startIndex, pageSize, whereClause, orderClause);
            PagedList<SimpleProductEntity> pagedExList = new PagedList<SimpleProductEntity>(entityList.Records, entityList.PageIndex, entityList.PageSize, entityList.TotalCount);

            return View(pagedExList);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productGuid"></param>
        /// <returns></returns>
        public ActionResult ProductDetails(string productGuid = "", string returnUrl="")
        {
            SimpleProductEntity entity = null;
            if (string.IsNullOrWhiteSpace(productGuid) || productGuid == Guid.Empty.ToString())
            {
                entity = new SimpleProductEntity();
            }
            else
            {
                entity = SimpleProductBLL.Instance.Get(productGuid);
            }

            this.PassParam("returnUrl", returnUrl);

            return View(entity);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductDetails(SimpleProductEntity entity,string returnUrl= "")
        {
            if (this.Request.Params["productIsTop"] != null)
            {
                entity.ProductIsTop = Logics.True;
            }
            else
            {
                entity.ProductIsTop = Logics.False;
            }

            if (this.Request.Params["CanUsable"] != null)
            {
                entity.CanUsable = Logics.True;
            }
            else
            {
                entity.CanUsable = Logics.False;
            }

            if (entity.ProductGuid == Guid.Empty)
            {
                SimpleProductBLL.Instance.Create(entity);
            }
            else
            {
                SimpleProductBLL.Instance.Update(entity);
            }

            if (string.IsNullOrWhiteSpace(returnUrl) == true)
            {
                returnUrl = Url.Action("ProductList");
            }
            return new RedirectResult(returnUrl);
        }

        public ActionResult ProductDelete(Guid productGuid)
        {
            LogicStatusInfo statusInfo = new LogicStatusInfo();
            statusInfo.IsSuccessful = SimpleProductBLL.Instance.Delete(productGuid);
            return Json(statusInfo);
        }
    }
}
